package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.MyApplication;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.MyFragmentPagerAdapter;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.NoReadMesBean;
import com.sx.zongzhi.bean.VersionBean;
import com.sx.zongzhi.fragment.CollectFragment;
import com.sx.zongzhi.fragment.MainPagerFragment;
import com.sx.zongzhi.fragment.MyFragment;
import com.sx.zongzhi.fragment.WorkFragment;
import com.sx.zongzhi.port.PermissionsResultListener;
import com.sx.zongzhi.presenter.NoReadMesPest;
import com.sx.zongzhi.presenter.VersionPest;
import com.sx.zongzhi.utils.AppConstants;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.Global;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.INoReadMesView;
import com.sx.zongzhi.view.IVersionView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;


public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener
        ,IVersionView ,INoReadMesView{

    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.rbt_MainPager)
    RadioButton rbt_MainPager;
    @BindView(R.id.rbt_Work)
    RadioButton rbt_Work;
    @BindView(R.id.rbt_Collect)
    RadioButton rbt_Collect;
    @BindView(R.id.rbt_My)
    RadioButton rbt_My;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tv_Num)
    TextView tvNum;
    @BindView(R.id.tvVersion)
    TextView tvVersion;
    private NoReadMesPest readMesPest;
    private Disposable disposable;

    private List<Fragment> fragments = new ArrayList<>();

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setViews();
        getNoReadMessage();
        new VersionPest(this).getVersion();
        performRequestPermissions(AppConstants.FORCE_REQUIRE_PERMISSIONS, 100, new PermissionsResultListener() {
           @Override
           public void onPermissionGranted() {
           }
           @Override
           public void onPermissionDenied() {
           }
       });
    }


    private void setViews() {
        fragments.add(new MainPagerFragment());
        fragments.add(new WorkFragment());
        fragments.add(new CollectFragment());
        fragments.add(new MyFragment());
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),
                fragments, null);
        viewPager.setAdapter(pagerAdapter);
        radioGroup.setOnCheckedChangeListener(this);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        rbt_MainPager.setChecked(true);
                        break;
                    case 1:
                        rbt_Work.setChecked(true);
                        break;
                    case 2:
                        rbt_Collect.setChecked(true);
                        break;
                    case 3:
                        rbt_My.setChecked(true);
                        break;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {}
        });

        setLeftImage(R.mipmap.icon_nav_position);
        setTitleName("综治信息平台");
        findViewById(R.id.imv_Left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,OrganizingActivity.class).
                        putExtra(Global.ACTIVITY_FRAGMENT,"MainPagerFragment"));
            }
        });
        setRightImage(R.mipmap.icon_nav_message);

        //订阅筛选组织机构
        disposable= MyRxBus.get().toObservable(MsgEvent.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent bean) throws Exception {
                        if ("MainActivity".equals(bean.getRequest())) {
                           if(bean.getType()==1){
                              getNoReadMessage();
                           }
                        }
                    }
                });

    }

    @Override
    protected void onDestroy() {
        if (disposable!=null){
            disposable.dispose();
        }
        super.onDestroy();
    }

    @OnClick({R.id.imv_Right})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.imv_Right:
               startActivity(new Intent(MainActivity.this,InBoxActivity.class));
                break;
        }
    }


    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId){
            case R.id.rbt_MainPager:
                viewPager.setCurrentItem(0);
                setTitleName("综治信息平台");
                setBackShow();
                break;
            case R.id.rbt_Work:
                viewPager.setCurrentItem(1);
                setTitleName("办公");
                setBackHide();
                break;
            case R.id.rbt_Collect:
                viewPager.setCurrentItem(2);
                setTitleName("信息采集");
                setBackHide();
                break;
            case R.id.rbt_My:
                viewPager.setCurrentItem(3);
                setTitleName("我的");
                setBackHide();
                break;

        }
    }

    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtils.showToast(MainActivity.this, "再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                MyRxBus.get().unregisterAll();
                MyApplication.instance.finishAllActivity();
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void getVersion(VersionBean versionBean) {
        if (versionBean!=null){
            if (versionBean.getVersion()> BaseUtil.getVersionCode(this)){
                tvVersion.setVisibility(View.VISIBLE);
            }else {
                tvVersion.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }


    public void getNoReadMessage() {
        if (readMesPest==null){
            readMesPest=new NoReadMesPest(this);
        }
        readMesPest.getNoReadMessage();
    }

    @Override
    public void getNoMesView(NoReadMesBean readMesBean) {
        int count=readMesBean.getPersonnelMessageNum();
        if (count>0){
            tvNum.setVisibility(View.VISIBLE);
            if (count>99){
                tvNum.setText("99+");
            }else {
                tvNum.setText(count+"");
            }
        }else {
            tvNum.setVisibility(View.GONE);
        }
    }
}
